package com.cet.designpatterns.factory.simplefactory.pizzastore.order;

import com.cet.designpatterns.factory.simplefactory.pizzastore.pizza.Pizza;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @program: algorithm
 * @description:
 * @author: 陈恩涛
 * @create: 2021-08-31 23:06
 **/
public class OrderPizza {

    /*public OrderPizza() {
        Pizza pizza = null;
        //订购披萨的类型
        String orderType;

        do {
            orderType = getType();
            if ("greek".equals(orderType)) {
                pizza = new GreekPizza();
                pizza.setName("希腊披萨");
            } else if ("cheese".equals(orderType)) {
                pizza = new BJCheesePizza();
                pizza.setName("奶酪披萨");
            } else if ("pepper".equals(orderType)) {
                pizza = new LDPepperPizza();
                pizza.setName("胡椒披萨");
            } else {
                break;
            }
            //输出Pizza制作的过程
            pizza.prepare();
            pizza.bake();
            pizza.cut();
            pizza.box();
        }while (true);
    }*/

    private SimplePizzaFactory simplePizzaFactory;
    Pizza pizza = null;

    public OrderPizza(SimplePizzaFactory simplePizzaFactory) {
        setSimplePizzaFactory(simplePizzaFactory);
    }

    public void setSimplePizzaFactory(SimplePizzaFactory simplePizzaFactory) {
        String orderType = "";
        this.simplePizzaFactory = simplePizzaFactory;
        do {
            orderType = getType();
            pizza = this.simplePizzaFactory.createPizza(orderType);
            if (pizza != null) {
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println("订购披萨失败！");
                break;
            }

        }while (true);
    }

    // 获取客户希望订购的披萨种类
    private String getType() {
        try {
            BufferedReader strin = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("input pizza type:");
            return strin.readLine();
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }
}
